﻿Public Class QueryBuilder

    Private _serialNumber As String
    Private _token As UInt32
    Private _queryHash As Hashtable

#Region "Properties"
    Public Property SerialNumber() As String
        Get
            Return _serialNumber
        End Get
        Set(ByVal value As String)
            _serialNumber = value
        End Set
    End Property

    Public Property Token() As UInt32
        Get
            Return _token
        End Get
        Set(ByVal value As UInt32)
            _token = value
        End Set
    End Property
#End Region

#Region "Constructors"
    Public Sub New(ByVal serialNumber As String, ByVal token As UInt32)
        _serialNumber = serialNumber
        _token = token
        _queryHash = New Hashtable
    End Sub
#End Region

#Region "Methods"
    Public Sub SetMessageId(ByVal messageId As UInt32)
        _queryHash("MessageId") = GetMessageIdParam(messageId)
    End Sub
    Public Sub SetNabcastId(ByVal nabcastId As UInt32)
        _queryHash("NabcastId") = GetNabcastIdParam(nabcastId)
    End Sub
    Public Sub SetNabcastTitle(ByVal nabcastTitle As String)
        _queryHash("NabcastTitle") = GetNabcastTitleParam(nabcastTitle)
    End Sub
    Public Sub SetRequestForEarPosition()
        _queryHash("RequestForEarPosition") = GetRequestForEarPosition()
    End Sub
    Public Sub SetApplicationId(ByVal applicationId As UInt32)
        _queryHash("ApplicationId") = GetApplicationIdParam(applicationId)
    End Sub
    Public Sub SetRightEarPosition(ByVal position As UInt32)
        _queryHash("RightEarPosition") = GetRightEarPositionParam(position)
    End Sub
    Public Sub SetLeftEarPosition(ByVal position As UInt32)
        _queryHash("LeftEarPosition") = GetLeftEarPositionParam(position)
    End Sub
    Public Sub SetVoice(ByVal voice As Lookups.VoiceEnum)
        _queryHash("Voice") = GetVoiceParam(voice)
    End Sub
    Public Sub SetMessageText(ByVal messageText As String)
        _queryHash("MessageText") = GetMessageTextParam(messageText)
    End Sub
    Public Sub SetChoreography(ByVal choreography As String)
        _queryHash("Choreography") = GetChoreographyParam(choreography)
    End Sub
    Public Sub SetChoreographyTitle(ByVal choreographyTitle As String)
        _queryHash("ChoreographyTitle") = GetChoreographyTitleParam(choreographyTitle)
    End Sub
    Public Sub SetMessageLifeInSeconds(ByVal messageLife As UInt32)
        _queryHash("MessageLifeInSeconds") = GetMessageLifeInSecondsParam(messageLife)
    End Sub
    Public Sub SetAction(ByVal action As Lookups.ActionEnum)
        _queryHash("Action") = GetActionParam(action)
    End Sub

    Public Sub ClearMessageId(ByVal messageId As UInt32)
        _queryHash.Remove("MessageId")
    End Sub
    Public Sub ClearNabcastId(ByVal nabcastId As UInt32)
        _queryHash.Remove("NabcastId")
    End Sub
    Public Sub ClearNabcastTitle(ByVal nabcastTitle As String)
        _queryHash.Remove("NabcastTitle")
    End Sub
    Public Sub ClearRequestForEarPosition()
        _queryHash.Remove("RequestForEarPosition")
    End Sub
    Public Sub ClearApplicationId(ByVal applicationId As UInt32)
        _queryHash.Remove("ApplicationId")
    End Sub
    Public Sub ClearRightEarPosition(ByVal position As UInt32)
        _queryHash.Remove("RightEarPosition")
    End Sub
    Public Sub ClearLeftEarPosition(ByVal position As UInt32)
        _queryHash.Remove("LeftEarPosition")
    End Sub
    Public Sub ClearVoice(ByVal voice As Lookups.VoiceEnum)
        _queryHash.Remove("Voice")
    End Sub
    Public Sub ClearMessageText(ByVal messageText As String)
        _queryHash.Remove("MessageText")
    End Sub
    Public Sub ClearChoreography(ByVal choreography As String)
        _queryHash.Remove("Choreography")
    End Sub
    Public Sub ClearChoreographyTitle(ByVal choreographyTitle As String)
        _queryHash.Remove("ChoreographyTitle")
    End Sub
    Public Sub ClearMessageLifeInSeconds(ByVal messageLife As UInt32)
        _queryHash.Remove("MessageLifeInSeconds")
    End Sub
    Public Sub ClearAction(ByVal action As Lookups.ActionEnum)
        _queryHash.Remove("Action")
    End Sub
#End Region

#Region "Overrides"
    Public Overrides Function ToString() As String
        Dim values As ICollection = _queryHash.Values
        Dim query As New System.Text.StringBuilder
        query.AppendFormat("{0}&{1}", GetSerialNumberParam(_serialNumber), GetTokenParam(_token))
        For Each value As String In values
            query.AppendFormat("&{0}", value)
        Next
        Return query.ToString
    End Function
#End Region

#Region "Shared Methods"
    Public Shared Function GetSerialNumberParam(ByVal serialNumber As String) As String
        Return String.Format("sn={0}", serialNumber)
    End Function

    Public Shared Function GetTokenParam(ByVal token As String) As String
        Return String.Format("token={0}", token)
    End Function

    Public Shared Function GetMessageIdParam(ByVal messageId As UInt32) As String
        Return String.Format("idmessage={0}", messageId)
    End Function

    Public Shared Function GetNabcastIdParam(ByVal nabcastId As UInt32) As String
        Return String.Format("nabcast={0}", nabcastId)
    End Function

    Public Shared Function GetNabcastTitleParam(ByVal nabcastTitle As String) As String
        Return String.Format("nabcasttitle={0}", nabcastTitle)
    End Function

    Public Shared Function GetRequestForEarPosition() As String
        Return "ears=ok"
    End Function

    Public Shared Function GetApplicationIdParam(ByVal applicationId As UInt32) As String
        Return String.Format("idapp={0}", applicationId)
    End Function

    Public Shared Function GetRightEarPositionParam(ByVal position As UInt32) As String
        Return String.Format("posright={0}", position)
    End Function

    Public Shared Function GetLeftEarPositionParam(ByVal position As UInt32) As String
        Return String.Format("posleft={0}", position)
    End Function

    Public Shared Function GetVoiceParam(ByVal voice As Lookups.VoiceEnum) As String
        Return System.String.Format("voice={0}", Lookups.Voices.GetVoice(voice))
    End Function

    Public Shared Function GetMessageTextParam(ByVal messageText As String) As String
        Return System.String.Format("tts={0}", messageText)
    End Function

    Public Shared Function GetChoreographyParam(ByVal choreography As String) As String
        Return System.String.Format("chor={0}", choreography)
    End Function

    Public Shared Function GetChoreographyTitleParam(ByVal choreographyTitle As String) As String
        Return System.String.Format("chortitle={0}", choreographyTitle)
    End Function

    Public Shared Function GetMessageLifeInSecondsParam(ByVal messageLife As UInt32) As String
        Return System.String.Format("ttlive={0}", messageLife)
    End Function

    Public Shared Function GetActionParam(ByVal action As Lookups.ActionEnum) As String
        Return System.String.Format("action={0}", CInt(action))
    End Function
#End Region

End Class
